----------------------------------------------------------------------------------
-- Company: 
-- Engineer: 
-- 
-- Create Date:    17:31:25 09/24/2013 
-- Design Name: 
-- Module Name:    multiplier_process - Behavioral 
-- Project Name: 
-- Target Devices: 
-- Tool versions: 
-- Description: 
--
-- Dependencies: 
--
-- Revision: 
-- Revision 0.01 - File Created
-- Additional Comments: 
--
----------------------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

-- Uncomment the following library declaration if using
-- arithmetic functions with Signed or Unsigned values
--use IEEE.NUMERIC_STD.ALL;

-- Uncomment the following library declaration if instantiating
-- any Xilinx primitives in this code.
--library UNISIM;
--use UNISIM.VComponents.all;

entity multiplier_process is
	 Port ( clock : in STD_LOGIC;
			  a : in  STD_LOGIC_VECTOR (31 downto 0);
           b : in  STD_LOGIC_VECTOR (31 downto 0);
			  carry: in STD_LOGIC;
			  sumIn : in  STD_LOGIC_VECTOR (31 downto 0);
			  sumOut : out  STD_LOGIC_VECTOR (63 downto 0);
			  addItemOne: out STD_LOGIC_VECTOR (31 downto 0);
			  addItemTwo: out STD_LOGIC_VECTOR (31 downto 0);
			  counterValue : in STD_LOGIC_VECTOR (4 downto 0);
			  counterReset : out STD_LOGIC);
end multiplier_process;

architecture Behavioral of multiplier_process is
begin
process (clock)
	variable aLast : STD_LOGIC_VECTOR (31 downto 0) := (others => '0');
	variable bLast : STD_LOGIC_VECTOR (31 downto 0) := (others => '0');
	variable itemOne : STD_LOGIC_VECTOR (31 downto 0) := (others => '0');
	variable itemTwo : STD_LOGIC_VECTOR (31 downto 0) := (others => '0');
	variable reset: STD_LOGIC := '1';
	variable sumIntermediate : STD_LOGIC_VECTOR (63 downto 0) := (others => '0');
begin
	if clock = '1' and clock'event then
		if a /= aLast or b /= bLast then
			reset := '0';
			aLast := a;
			bLast := b;
			sumIntermediate := "00000000000000000000000000000000" & b;
		end if;
		if reset = '0' then
			-- Do components for adding first
			if sumIntermediate(0) = '1' then
				itemOne := a;
			else
				itemOne := "00000000000000000000000000000000";
			end if;
			itemTwo := sumIntermediate (63 downto 32);
			-- Then shift
			sumIntermediate := carry & sumIn & sumIntermediate(31 downto 1);
		end if;
		if counterValue = "11111" then
			reset := '1';
		end if;
	end if;
	sumOut <= sumIntermediate;
	counterReset <= reset;
	additemOne <= itemOne;
	additemTwo <= itemTwo;	
end process;
end Behavioral;

